﻿<template>
    <div style="height:100%;">
        <Row>
            <Col span="24" class="col-margin">
            <!--根据需求删除不不要查询条件-->
            填报年份：
            <Select v-model="searchModel.FillInYear" placeholder="全部" :label-in-value="true" @on-change="initReport()" style="width:200px">
                <Option v-for="item in yearList" :value="item.DictValue" :key="item.DictValue">{{ item.DictText }}</Option>
            </Select>
            <!-- <Button icon="ios-search" type="ghost" @click="getPageData()">查询</Button> -->
            <Button icon="ios-download" type="info" @click="ExcelDown()" style="float:right;margin-right:20px" v-show="isCreated&&ReportStatus >1">数据导出</Button>
            <Upload :on-success="handleSuccess" :action="GetPostUrl()" style="float:right;margin-right:20px" v-show="isCreated&&ReportStatus <3">
                <Button type="success" icon="ios-cloud-upload-outline">数据导入</Button>
            </Upload>
            <Button icon="ios-download" type="warning" @click="TmpDown()" style="float:right;margin-right:20px">模板下载</Button>
            </Col>
        </Row>
        <div v-show="isCreated">        
        <Row>
            <Col span="24" class="col-margin">
            <div id="griddiv" style="overflow-y:auto" v-bind:style="{ height: clientHeight + 'px' }">
                <Form ref="dataModel" :model="DataModel" :rules="ruleValidate" > 
                <table  class="gridtable">
                     <caption>
                        <span style="float:left;font-size:20px;color:#050505">{{searchModel.FillInYear}}马铃薯生产经营实体情况表</span>
                        <span style="float:right" v-show="ReportStatus==0">[<a @click="ReasonShow=true">未通过原因</a>]</span>
                        <div style="float:right;margin-right:5px;">当前状态:
                            <span v-if="ReportStatus==0" class="Error">退回重报</span> 
                            <span v-else-if="ReportStatus==1" class="NoReport">未填写</span>
                            <span v-else-if="ReportStatus==2" class="AlreadyReport">已填写</span>
                            <span v-else-if="ReportStatus==3" class="UpReport">已上报</span>
                            <span v-else-if="ReportStatus==4" class="Success">审核通过</span>               
                            <span v-else class="Error">异常</span>               
                        </div>
                    </caption>
                    <thead>
                    <tr>
                        <th style="width:45%">名称</th>
                        <th style="width:20%">计量单位</th>
                        <th style="width:35%">数量</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <th style="text-align:left;">一、马铃薯市场个数</th>
                        <th>个</th>
                        <th>
                            <Poptip trigger="hover" title="上年数据" :content="oldDataModel.MarketNum"> 
                                <Form-item prop="MarketNum">                                     
                                    <Button long>{{DataModel.MarketNum}}</Button> 
                                    <td style="display:none">{{DataModel.MarketNum = DataModel.CdxMarket*1+DataModel.ZypfMarket*1+DataModel.QyxzhpfMarket*1}}</td>
                                </Form-item>
                            </Poptip>
                        </th>
                    </tr>
                    <tr>
                        <td style="padding-left:10em">其中：产地性市场</td>
                        <td style="text-align:center;">个</td>
                        <td>
                            <Poptip trigger="focus" title="上年数据" :content="oldDataModel.CdxMarket"> 
                                <Form-item prop="CdxMarket">  
                                    <Input v-model="DataModel.CdxMarket"></Input>
                                </Form-item>
                            </Poptip>
                        </td>
                    </tr>
                    <tr>
                        <td style="padding-left:13em">专业批发市场</td>
                        <td style="text-align:center;">个</td>
                        <td>
                            <Poptip trigger="focus" title="上年数据" :content="oldDataModel.ZypfMarket"> 
                                <Form-item prop="ZypfMarket">  
                                    <Input v-model="DataModel.ZypfMarket"></Input>
                                </Form-item>
                            </Poptip>
                        </td>
                    </tr>
                    <tr>
                        <td style="padding-left:13em">区域性综合批发市场</td>
                        <td style="text-align:center;">个</td>
                        <td>
                            <Poptip trigger="focus" title="上年数据" :content="oldDataModel.QyxzhpfMarket"> 
                                <Form-item prop="QyxzhpfMarket">  
                                    <Input v-model="DataModel.QyxzhpfMarket"></Input>
                                </Form-item>
                            </Poptip>
                        </td>
                    </tr>
                    <tr>
                        <th style="text-align:left;">二、马铃薯企业个数</th>
                        <th>个</th>
                        <th>
                            <Poptip trigger="hover" title="上年数据" :content="oldDataModel.FruCompanyNum"> 
                                <Form-item prop="FruCompanyNum">  
                                     <Button long>{{DataModel.FruCompanyNum}}</Button>
                                    <td style="display:none">{{DataModel.FruCompanyNum = DataModel.ZzNum_country*1+DataModel.ZzNum_pro*1+DataModel.ZzNum_city*1+DataModel.ZzNum_county*1+DataModel.ZzNum_other*1}}</td>
                                </Form-item>
                            </Poptip>
                        </th>
                    </tr>
                    <tr>
                        <td style="padding-left:10em">其中：国家级龙头企业</td>
                        <td style="text-align:center;">个</td>
                        <td>
                            <Poptip trigger="focus" title="上年数据" :content="oldDataModel.ZzNum_country"> 
                                <Form-item prop="ZzNum_country">  
                                    <Input v-model="DataModel.ZzNum_country"></Input>
                                </Form-item>
                            </Poptip>
                        </td>
                    </tr>
                    <tr>
                        <td style="padding-left:13em;">省级龙头企业</td>
                        <td style="text-align:center;">个</td>
                        <td>
                            <Poptip trigger="focus" title="上年数据" :content="oldDataModel.ZzNum_pro"> 
                                <Form-item prop="ZzNum_pro">  
                                    <Input v-model="DataModel.ZzNum_pro"></Input>
                                </Form-item>
                            </Poptip>
                        </td>
                    </tr>
                    <tr>
                        <td style="padding-left:13em;">州（市）级龙头企业</td>
                        <td style="text-align:center;">个</td>
                        <td>
                            <Poptip trigger="focus" title="上年数据" :content="oldDataModel.ZzNum_city"> 
                                <Form-item prop="ZzNum_city">  
                                    <Input v-model="DataModel.ZzNum_city"></Input>
                                </Form-item>
                            </Poptip>
                        </td>
                    </tr>
                    <tr>
                        <td style="padding-left:13em;">县（市、区）级龙头企业</td>
                        <td style="text-align:center;">个</td>
                        <td>
                            <Poptip trigger="focus" title="上年数据" :content="oldDataModel.ZzNum_county"> 
                                <Form-item prop="ZzNum_county">  
                                    <Input v-model="DataModel.ZzNum_county"></Input>
                                </Form-item>
                            </Poptip>
                        </td>
                    </tr>
                    <tr>
                        <td style="padding-left:13em;">其他</td>
                        <td style="text-align:center;">个</td>
                        <td>
                            <Poptip trigger="focus" title="上年数据" :content="oldDataModel.ZzNum_other"> 
                                <Form-item prop="ZzNum_other">  
                                    <Input v-model="DataModel.ZzNum_other"></Input>
                                </Form-item>
                            </Poptip>
                        </td>
                    </tr>                    
                    <tr>
                        <th colspan="3" style="text-align:left;">三、马铃薯农民合作社</th>                       
                    </tr>
                    <tr>
                        <td style="padding-left:13em;">1.农民合作社数量</td>
                        <td style="text-align:center;">个</td>
                        <td><Poptip trigger="focus" title="上年数据" :content="oldDataModel.HzNum"><Form-item prop="HzNum"><Input v-model="DataModel.HzNum"></Input></Form-item></Poptip></td>
                    </tr>
                    <tr>
                        <td style="padding-left:13em;">2.参加农民合作社农户数</td>
                        <td style="text-align:center;">户</td>
                        <td><Poptip trigger="focus" title="上年数据" :content="oldDataModel.HznhNum"><Form-item prop="NhhzNum"><Input v-model="DataModel.HznhNum"></Input></Form-item></Poptip></td>
                    </tr>
                    <tr>
                        <td style="padding-left:13em;">3.农民专业合作组织经营面积</td>
                        <td style="text-align:center;">亩</td>
                        <td><Poptip trigger="focus" title="上年数据" :content="oldDataModel.NmhzArea"><Form-item prop="NmhzArea"><Input v-model="DataModel.NmhzArea"></Input></Form-item></Poptip></td>
                    </tr>
                    <tr>
                        <td style="padding-left:15em;">其中：流转土地面积</td>
                        <td style="text-align:center;">亩</td>
                        <td><Poptip trigger="focus" title="上年数据" :content="oldDataModel.HzlzArea"><Form-item prop="HzlzArea"><Input v-model="DataModel.HzlzArea"></Input></Form-item></Poptip></td>
                    </tr>
                    <tr>
                        <td style="padding-left:13em;">4.农民合作社年销售收入</td>
                        <td style="text-align:center;">万元</td>
                        <td><Poptip trigger="focus" title="上年数据" :content="oldDataModel.NmhzYincome"><Form-item prop="NmhzYincome"><Input v-model="DataModel.NmhzYincome"></Input></Form-item></Poptip></td>
                    </tr>                   
                    </tbody>
                </table>
                </Form>
            </div>
            <div class="divButton">
                    <Button type="primary" @click="save('dataModel')" :disabled="ReportStatus < 3 ? false:true ">保存</Button>
                    <Button type="primary" @click="DataUp()" :disabled="ReportStatus == 2 ? false:true">上报</Button>
                </div>
                <Alert  type="error">
                    填报时间：每年1月10日
                    <p> 填报说明：1.全省范围内从乡（镇）推广站起，逐级全面统计，由州（市）汇总后上报。</p> 
                    <span slot="desc"></span>
                </Alert>
            </Col>
        </Row>
        </div>
        <div v-show="!isCreated" class="NoCreatedDiv">
            <h1>{{ReportLine}}</h1>
        </div>
        <Modal v-model="ReasonShow" :mask-closable="false" width="500"> 
            <p slot="header" class="title">
                <Icon type="ios-paper" size="20"></Icon>
                <span style="font-size: 20px">未通过原因</span>
            </p>
            <p slot="close">
                <Icon type="close-circled" color="red" size=25></Icon>
            </p>
            <div class="reasonDiv"><Input v-model="Opinion" type="textarea" readonly :autofocus="true" :rows="10"  ></Input></div>
            <p slot="footer"></p>
        </Modal>          
    </div>
</template>
<script>
export default {
    data() {
        const validateData = (rule, value, callback) => {
            var temp = /^[0-9]\d*(\.\d+)?$/;
            if (value||value==0) {
                if (temp.test(value) == false) {
                    callback(new Error('必须是数字！'));
                }
                else {
                    callback();
                }
            } else {
                callback(new Error('不能为空！'));
            }
        };
        return {
            isCreated:false,//是否需要显示报表
            ReportLine:'',//不显示报表时所显示文字
            ReasonShow:false,
            showType: false,//编辑框弹出状态
            yearList: this.GetGroupDict("ReportYear"),
            ReportId: "",
            ReportStatus: "", 
            Opinion:'',
            AreaCode: this.GetlocaStorage("potUserinfo").AreaCode,
            ruleValidate: {//验证模型
                //ReportId:[{ validator: validateData, trigger: 'blur' }], 
                MarketNum:{ validator: validateData, trigger: 'blur' }, 
                CdxMarket:{ validator: validateData, trigger: 'blur' }, 
                ZypfMarket:{ validator: validateData, trigger: 'blur' }, 
                QyxzhpfMarket:{ validator: validateData, trigger: 'blur' }, 
                FruCompanyNum:{ validator: validateData, trigger: 'blur' }, 
                ZzNum_country:{ validator: validateData, trigger: 'blur' }, 
                ZzNum_pro:{ validator: validateData, trigger: 'blur' }, 
                ZzNum_city:{ validator: validateData, trigger: 'blur' }, 
                ZzNum_county:{ validator: validateData, trigger: 'blur' }, 
                ZzNum_other:{ validator: validateData, trigger: 'blur' }, 
                HzNum:{ validator: validateData, trigger: 'blur' }, 
                HznhNum:{ validator: validateData, trigger: 'blur' }, 
                NmhzArea:{ validator: validateData, trigger: 'blur' }, 
                HzlzArea:{ validator: validateData, trigger: 'blur' }, 
                NmhzYincome:{ validator: validateData, trigger: 'blur' }, 
            },
                DataModel: {//编辑模型
                    Id: '',
                    ReportId:'', 
                    MarketNum:'', 
                    CdxMarket:'', 
                    ZypfMarket:'', 
                    QyxzhpfMarket:'', 
                    FruCompanyNum:'', 
                    ZzNum_country:'', 
                    ZzNum_pro:'', 
                    ZzNum_city:'', 
                    ZzNum_county:'', 
                    ZzNum_other:'', 
                    HzNum:'', 
                    HznhNum:'', 
                    NmhzArea:'', 
                    HzlzArea:'', 
                    NmhzYincome:'', 
                },
                oldDataModel: {
                    Id: '',
                    ReportId:'', 
                    MarketNum:'', 
                    CdxMarket:'', 
                    ZypfMarket:'', 
                    QyxzhpfMarket:'', 
                    FruCompanyNum:'', 
                    ZzNum_country:'', 
                    ZzNum_pro:'', 
                    ZzNum_city:'', 
                    ZzNum_county:'', 
                    ZzNum_other:'', 
                    HzNum:'', 
                    HznhNum:'', 
                    NmhzArea:'', 
                    HzlzArea:'', 
                    NmhzYincome:'', 
                },

            
            clientHeight: window.innerHeight - 183,//表格默认高度
            //查询模型：根据需求删除不不要查询条件
            searchModel: {  
                ReportCode:'PotProsecution',                       
                FillInYear:new Date().getFullYear()+'',
                PageNumber: 1, PageSize: 20
            },
            
        }
    },
    mounted() {
        //table高度计算方法
        const that = this;
        window.onresize = () => {
            return (() => {
                that.clientHeight = window.innerHeight - 183;
            })()
        };
    },
    methods: {        
        getPageData() {
            var self = this;
            this.$http.get('PotProsecution', {params:{AreaCode:this.AreaCode,FillInYear:this.searchModel.FillInYear}}).then(function (res) {
                if (res.data.status === 1) {
                    if(res.data.model.DataModel)
                    self.DataModel = res.data.model.DataModel;
                    if(res.data.model.OldDataModel)
                    self.oldDataModel = res.data.model.OldDataModel;
                    self.ReportStatus = res.data.model.ReportStatus; 
                    self.ReportId = res.data.model.ReportId;  
                    self.Opinion = res.data.model.Opinion; 
                    self.isCreated = true;                
                }
                else {
                    self.isCreated = false;
                    self.ReportLine = '报表未生成！'
                    self.ReportStatus = null;
                    self.$Modal.error({ title: '提示信息', content: res.data.message });
                }
            });
        },        
        //保存方法
        save(modelname) {
            var self = this;
            this.$refs[modelname].validate((valid) => {
                if (valid) { 
                    this.$http.put('PotProsecution', this.DataModel).then(
                        function (res) {
                            if (res.data.status == 1) {
                                self.getPageData();                                
                                self.$Notice.success({ title: '保存成功！', desc: false });
                            }
                            else {
                                self.$Modal.warning({ title: "提示信息", content: res.data.message });
                            }
                        });
                }
                else{
                    self.$Modal.error({ title: "提示信息", content: '无法保存，请检查是否有未填项！' });
                }
                
            });
        },
        checkData(){
            if(this.DataModel.MarketNum != this.DataModel.CdxMarket*1+this.DataModel.ZypfMarket*1+this.DataModel.QyxzhpfMarket*1){
            this.toAlertCheckResult( '马铃薯市场个数不等于其子项之和！');
            return false;
            }
            else if(this.DataModel.FruCompanyNum != this.DataModel.ZzNum_country*1+this.DataModel.ZzNum_pro*1+this.DataModel.ZzNum_city*1+this.DataModel.ZzNum_county*1+this.DataModel.ZzNum_other*1){
                this.toAlertCheckResult('马铃薯企业个数不等于其子项之和！');
                return false;
            }
            else{
                return true;
            }
        },
        toAlertCheckResult(message){  
            this.$Modal.warning({
                title:'提示信息',
                content:message
            });
        },
        //数据上报
        DataUp() {
            var self = this;
            let model = new Object();
            model.Id = this.ReportId;
            model.Status = 3;
            model.Opinion = '';
            if(this.checkData()){
                self.$Modal.confirm({
                    title: '提示信息',
                    content: '<p>数据上报后，不能再更改，是否继续上报？</p>',
                    onOk: () => {
                        this.$http.post("Report/PostUpStatus", model).then(
                            function (res) {
                                if (res.data.status == 1) {                               
                                    self.$Notice.success({ title: '数据上报成功！', desc: false });
                                    self.getPageData();
                                }
                                else {
                                    self.$Modal.error({ title: "提示信息", content: "数据上报失败!" });
                                }
                            }
                        );
                    }
                });
            }            
        },
         //模板下载
        TmpDown:function(){
            window.open(this.$http.defaults.baseURL + "Report/GetTemplateData?ReportCode=" + this.searchModel.ReportCode);
        },
         //导入
        GetPostUrl() {
            var url = this.$http.defaults.baseURL + "PotProsecution/PostImportData?ReportId=" + this.ReportId;
            return url;
        },
        //导入成功反馈事件
        handleSuccess(res, file) {
            var self = this;
            self.getPageData();
            if(res.status == -1){
                self.$Modal.warning({
                    title: "提示信息",
                    content: res.message
                });
            }else{
                self.$Notice.success({
                    title: '数据导入成功！',
                    desc: false
                });
            }            
        },
        //导出
        ExcelDown: function () {           
            window.open(this.$http.defaults.baseURL + "PotProsecution/GetExcelData?FillInYear=" + this.searchModel.FillInYear + "&&AreaCode=" + this.AreaCode);
        },
        initReport(){
            var today = new Date();
            let currentYear = today.getFullYear();
            let currentMonth = today.getMonth() + 1;
            if(this.searchModel.FillInYear*1 == currentYear &&currentMonth==12){
               this.getPageData();
            }
            else if(this.searchModel.FillInYear*1 == currentYear-1 && currentMonth >= 1 && currentMonth <= 5){
                this.getPageData();
            }
            else if(this.searchModel.FillInYear < currentYear){
                this.getPageData();
            }
            else {
                this.isCreated = false;
                this.ReportLine = '不在填报时间内！';
            }
        }
       
    },
    //创建Vue页面方法
    created: function () {
        //创业Vue前调用获取this.datas: {},//table数据对象
        this.initReport();
    },
    watch: {
        //需要监听的在此处新增监听对象
    }
}
</script>
